package com.example.yls.android_service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
private Button btnStar;
private Button btnStop;
private Button btnBind;
private ServiceConnection serviceConnection;
private Button btnunBind;
private MyBinder binder;
private Button btnAdd;
private Button btntoUperCase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        serviceConnection = new ServiceConnection(){

            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.e("MainActivity","onService");
                binder = (MyBinder) iBinder;
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                Log.e("MainActivity","onService");
            }
        };

        btnStar = findViewById(R.id.btn_star);
        btnStop = findViewById(R.id.btn_stop);
        btnBind = findViewById(R.id.btn_binder);
        btnunBind = findViewById(R.id.btn_unbinder);
        btnAdd = findViewById(R.id.btn_binder_add);
        btntoUperCase = findViewById(R.id.btn_binder_tuUperCase);

        btnStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                startService(intent);
            }
        });
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                stopService(intent);
            }
        });
        btnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                bindService(intent,serviceConnection,BIND_AUTO_CREATE);

            }
        });
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int result = binder.add(100,200);
                Log.e("binder.add","result= " + result);

            }
        });
        btntoUperCase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String str = binder.toUpperCase("hello world");
                Log.e("binder.tuUperCase","str = " + str);
            }
        });
        btnunBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unbindService(serviceConnection);
            }
        });
    }
}
